package com.kunbo.cn.controller.core.sale;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.util.retu.ReturnBean;

@RestController
@RequestMapping("/saleWaybill/")
@Transactional
public class SaleWaybillManagerController {
	
	@Autowired
	private JdbcTemplate jdbc;
	@RequestMapping("startSwb")
	public ReturnBean startSwb(String orderNumber,Double volume,Integer id) {
		//1.根据订单编号获取订单
		String sql = "select contr_margin from business_sale_order_manage where sale_order_code = '"+orderNumber+"'";
		Map<String,Object> obj = jdbc.queryForMap(sql);
		double d=0.00;
		if(null!=obj) {
			d = (float) obj.get("contr_margin");
		}else {
			return new ReturnBean(false,"参数异常，请检测");
		}
        double margin = d-volume;
        String sqlUp = "update business_sale_order_manage set contr_margin="+margin+" where sale_order_code = '"+orderNumber+"'";
		String sqlUp1 = "update business_waybill_manage set state='4'  where id= "+id+"";
		int a = jdbc.update(sqlUp);
		int b = jdbc.update(sqlUp1);
		if(a+b>1) {
			return new ReturnBean(true,"运单启用成功");
		}
		return new ReturnBean(false,"启用失败");
		
	}
	
	//运单作废
	@RequestMapping("cancelSaleWayBill")
	public ReturnBean cancelSaleWayBill(String tableName ,Integer id) {
		if("".equals(tableName) || null==id) {
			return new ReturnBean(false,"参数有误，请检测");
		}
		Integer r = jdbc.update("update "+tableName+" set state = '5'  where id = "+id+"");
		if(r>0) {
			return new ReturnBean(true,"操作成功");
		}else {
			return new ReturnBean(false,"执行失败");
		}
		
		
	}
	
	//运单完成
	@RequestMapping("finishSaleWayBill")
	public ReturnBean finishSaleWayBill(String tableName ,Integer id) {
		if("".equals(tableName) || null==id) {
			return new ReturnBean(false,"参数有误，请检测");
		}
		Integer r = jdbc.update("update "+tableName+" set state = '3'  where id = "+id+"");
		if(r>0) {
			return new ReturnBean(true,"操作成功");
		}else {
			return new ReturnBean(false,"执行失败");
		}
		
		
	}

}
